package com.ssy.lingxi.order.serviceimpl.feign;

import com.ssy.lingxi.common.constant.order.OrderTypeEnum;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.order.entity.OrderDO;
import com.ssy.lingxi.order.service.feign.IPurchaseFeignService;
import com.ssy.lingxi.purchase.api.dto.request.RequisitionProductReq;
import com.ssy.lingxi.purchase.api.dto.request.RequisitionQuantityDeductReq;
import com.ssy.lingxi.purchase.api.feign.PurchaseRequisitionFeign;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * 采购服务Feign接口实现类
 * @author wyx
 * @version 2.0.0
 * @date 2021-11-08
 */
@Service
public class PurchaseFeignServiceImpl implements IPurchaseFeignService {
    @Resource
    private PurchaseRequisitionFeign purchaseRequisitionFeign;

    /**
     * 请购单转请购订单扣减请购数量、增加已转订单数量
     * @param order 请购单订单
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> requisitionQuantityDeduct(OrderDO order) {
        if (!OrderTypeEnum.REQUISITION_TO_PURCHASE.getCode().equals(order.getOrderType())) {
            return Wrapper.success();
        }
        RequisitionQuantityDeductReq req = new RequisitionQuantityDeductReq();
        req.setRequisitionId(order.getQuoteId());
        req.setProducts(order.getProducts().stream().map(product -> {
            RequisitionProductReq productReq = new RequisitionProductReq();
            productReq.setProductId(product.getProductId());
            productReq.setTransferQuantity(product.getQuantity());
            return productReq;
        }).collect(Collectors.toList()));

        return purchaseRequisitionFeign.requisitionQuantityDeduct(req);
    }
}
